using System.Text.Json.Serialization;

namespace Uantek.Device.Communication.Models
{
    /// <summary>
    /// 通信结果模型
    /// </summary>
    public class CommunicationResult
    {
        /// <summary>
        /// 操作是否成功
        /// </summary>
        [JsonPropertyName("success")]
        public bool Success { get; set; }

        /// <summary>
        /// 结果消息
        /// </summary>
        [JsonPropertyName("message")]
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 错误代码（当Success为false时）
        /// </summary>
        [JsonPropertyName("errorCode")]
        public string? ErrorCode { get; set; }

        /// <summary>
        /// 异常信息（当Success为false时）
        /// </summary>
        [JsonPropertyName("exception")]
        public Exception? Exception { get; set; }

        /// <summary>
        /// 操作时间戳
        /// </summary>
        [JsonPropertyName("timestamp")]
        public DateTime Timestamp { get; set; } = DateTime.Now;

        /// <summary>
        /// 操作耗时（毫秒）
        /// </summary>
        [JsonPropertyName("elapsedMilliseconds")]
        public long ElapsedMilliseconds { get; set; }

        /// <summary>
        /// 扩展数据
        /// </summary>
        [JsonPropertyName("extendedData")]
        public Dictionary<string, object> ExtendedData { get; set; } = new();

        /// <summary>
        /// 创建成功结果
        /// </summary>
        /// <param name="message">消息</param>
        /// <returns>成功结果</returns>
        public static CommunicationResult CreateSuccess(string message = "操作成功")
        {
            return new CommunicationResult
            {
                Success = true,
                Message = message
            };
        }

        /// <summary>
        /// 创建失败结果
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="errorCode">错误代码</param>
        /// <param name="exception">异常</param>
        /// <returns>失败结果</returns>
        public static CommunicationResult CreateFailure(string message, string? errorCode = null, Exception? exception = null)
        {
            return new CommunicationResult
            {
                Success = false,
                Message = message,
                ErrorCode = errorCode,
                Exception = exception
            };
        }

        /// <summary>
        /// 创建成功结果（简化方法名）
        /// </summary>
        /// <param name="message">消息</param>
        /// <returns>成功结果</returns>
        public static CommunicationResult Ok(string message = "操作成功")
        {
            return CreateSuccess(message);
        }

        /// <summary>
        /// 创建失败结果（简化方法名）
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="errorCode">错误代码</param>
        /// <param name="exception">异常</param>
        /// <returns>失败结果</returns>
        public static CommunicationResult Failed(string message, string? errorCode = null, Exception? exception = null)
        {
            return CreateFailure(message, errorCode, exception);
        }
    }

    /// <summary>
    /// 带返回数据的通信结果模型
    /// </summary>
    /// <typeparam name="T">返回数据类型</typeparam>
    public class CommunicationResult<T> : CommunicationResult
    {
        /// <summary>
        /// 返回数据
        /// </summary>
        [JsonPropertyName("data")]
        public T? Data { get; set; }

        /// <summary>
        /// 创建成功结果
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="message">消息</param>
        /// <returns>成功结果</returns>
        public static CommunicationResult<T> CreateSuccess(T? data, string message = "操作成功")
        {
            return new CommunicationResult<T>
            {
                Success = true,
                Message = message,
                Data = data
            };
        }

        /// <summary>
        /// 创建失败结果
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="errorCode">错误代码</param>
        /// <param name="exception">异常</param>
        /// <returns>失败结果</returns>
        public static new CommunicationResult<T> CreateFailure(string message, string? errorCode = null, Exception? exception = null)
        {
            return new CommunicationResult<T>
            {
                Success = false,
                Message = message,
                ErrorCode = errorCode,
                Exception = exception
            };
        }

        /// <summary>
        /// 创建成功结果（简化方法名）
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="message">消息</param>
        /// <returns>成功结果</returns>
        public static CommunicationResult<T> Ok(T? data, string message = "操作成功")
        {
            return CreateSuccess(data, message);
        }

        /// <summary>
        /// 创建失败结果（简化方法名）
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="errorCode">错误代码</param>
        /// <param name="exception">异常</param>
        /// <returns>失败结果</returns>
        public static new CommunicationResult<T> Failed(string message, string? errorCode = null, Exception? exception = null)
        {
            return CreateFailure(message, errorCode, exception);
        }
    }
}